package com.activenetwork.smlite.testcase.events.createEvents.selectComponents;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.custom.uielement.decorate.IdentifierFactory;
import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.testcase.events.createevents.selectcomponents.selectcomponents004.SelectComponents004DataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.page.event.EventDetailView.EventSourceSubview;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class SelectComponents004 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(SelectComponents004.class);
	private SelectComponents004DataType data;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup() {	
		try{
			ScriptDataBindHelper helper = new ScriptDataBindHelper(SelectComponents004.class);
			data = helper.bind(SelectComponents004DataType.class);
		}catch(IOException e){
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
	}
	
	@Test
	public void Verify_left_navigation_works_correctly(){
		loginBusiness.login();
		EventBusiness eventBusiness = new EventBusiness();
		
		// Delete the test data if exists
		// then add a new event
		String eventTitle = data.getEvent().getBasic().getTitle();
		try{
			eventBusiness.deleteEvent(eventTitle, data.getEvent());
		}catch(RuntimeException e){
			logger.info("No need to delete test data since " + eventTitle + "does not exist");
		}
		eventBusiness.addEvent(data.getEvent());
		
		// Verify selected components enables 
		// the left navigation links correspondingly on sourcing page
		WebDriver driver = EnvContext.webDriverTL.get();
		EventBasicType.Components components = data.getEvent().getBasic().getComponents();
		if(components.getSourcing()!=null){
			verifyComponentEnableLink(driver, "sourcing", "Sourcing");
		}
		if(components.getSocialMedia()!=null){
			verifyComponentEnableLink(driver, "socialMedia", "Social Media");
		}
		if(components.getWebsite()!=null){
			verifyComponentEnableLink(driver, "website", "Website");
		}
		if(components.getEmail()!=null){
			verifyComponentEnableLink(driver, "email", "Email");
		}
		if(components.getAttendeeDirectories()!=null){
			verifyComponentEnableLink(driver, "attendeeDirectories", "Attendee Directories");
		}
		if(components.getRegistrationForm()!=null){		
			verifyComponentEnableLink(driver, "registratonForm", "Registration Form");
		}
		if(components.getThemeDesigner()!=null){			
			verifyComponentEnableLink(driver, "themeDesigner", "Theme Designer");
		}
		if(components.getMobile()!=null){		
			verifyComponentEnableLink(driver, "mobile", "Mobile");
		}
		if(components.getBadgesLabels()!=null){		
			verifyComponentEnableLink(driver, "badgeLabel", "Badeges & Label");
		}
		if(components.getKiosk()!=null){
			verifyComponentEnableLink(driver, "kiosk", "Kiosk");
		}
		if(components.getReports()!=null){
			verifyComponentEnableLink(driver, "reports", "Reports");
		}
		if(components.getCustomReports()!=null){
			verifyComponentEnableLink(driver, "customReports", "Custom Reports");
		}
		if(components.getSurveys()!=null){
			verifyComponentEnableLink(driver, "surveys", "Surveys");
		}
		
		// Go back event basic page and verify left navigation
		// Sourcing link goes to sourcing page
		EventSourceSubview eventSourceSubview = PageLoader.load(EventSourceSubview.class);
		EventBasicSubview eventBasicSubview = eventSourceSubview.goBackBasicSubview();
		eventSourceSubview = eventBasicSubview.navigateSourcing();
		
		loginBusiness.logout();
		FailureAssertion.pop(SelectComponents004.class);
	}
	
	private void verifyComponentEnableLink(WebDriver driver, String jsonFieldName, String componentName){
		By leftNavigationLinkBy = IdentifierFactory.createBy("EventLeftNavigation", jsonFieldName);
		String value = driver.findElement(leftNavigationLinkBy).getAttribute("class");
		if(StringUtils.containsIgnoreCase(value, "hide")){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					SelectComponents004.class.getName(), 
					"Verify check on component display left navigation", 
					componentName + " checked on but left navigation does not display"));
		}
		
	}
}
